<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 6.3.0">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.2/css/all.min.css" integrity="sha256-CTSx/A06dm1B063156EVh15m6Y67pAjZZaQc89LLSrU=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.24/fancybox/fancybox.css" integrity="sha256-vQkngPS8jiHHH0I6ABTZroZk8NPZ7b+MUReOFE9UsXQ=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"rjyblog.gitee.io","root":"/","images":"/images","scheme":"Pisces","darkmode":false,"version":"8.18.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":true,"style":null},"fold":{"enable":false,"height":500},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="本文创建了一个独立进程的service，通过bind方式来绑定服务，并通过binder与service进行交互。基于此来分析binder交互机制。 Service的创建与使用定义aidl接口： 123456789&#x2F;&#x2F;IRemote.aidlinterface IRemote &amp;#123;    void setCallback(RemoteCallback callback);&amp;#125;&#x2F;&#x2F;Re">
<meta property="og:type" content="article">
<meta property="og:title" content="通过自定义Service理解Binder">
<meta property="og:url" content="https://rjyblog.gitee.io/posts/1b96348b.html">
<meta property="og:site_name" content="任建勇的博客">
<meta property="og:description" content="本文创建了一个独立进程的service，通过bind方式来绑定服务，并通过binder与service进行交互。基于此来分析binder交互机制。 Service的创建与使用定义aidl接口： 123456789&#x2F;&#x2F;IRemote.aidlinterface IRemote &amp;#123;    void setCallback(RemoteCallback callback);&amp;#125;&#x2F;&#x2F;Re">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2023-10-22T23:50:48.000Z">
<meta property="article:modified_time" content="2023-11-18T00:32:16.475Z">
<meta property="article:author" content="Jason">
<meta property="article:tag" content="Android">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="https://rjyblog.gitee.io/posts/1b96348b.html">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://rjyblog.gitee.io/posts/1b96348b.html","path":"posts/1b96348b.html","title":"通过自定义Service理解Binder"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>通过自定义Service理解Binder | 任建勇的博客</title>
  








  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">任建勇的博客</p>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#Service%E7%9A%84%E5%88%9B%E5%BB%BA%E4%B8%8E%E4%BD%BF%E7%94%A8"><span class="nav-text">Service的创建与使用</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#IRemote-aidl%E7%9A%84java%E5%AE%9E%E7%8E%B0"><span class="nav-text">IRemote.aidl的java实现</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Service%E7%BB%91%E5%AE%9A%E6%B5%81%E7%A8%8B"><span class="nav-text">Service绑定流程</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#writeStrongBinder"><span class="nav-text">writeStrongBinder</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Jason"
      src="/images/avatar.png">
  <p class="site-author-name" itemprop="name">Jason</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">66</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">13</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">23</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://rjyblog.gitee.io/posts/1b96348b.html">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.png">
      <meta itemprop="name" content="Jason">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="任建勇的博客">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="通过自定义Service理解Binder | 任建勇的博客">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          通过自定义Service理解Binder
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2023-10-23 07:50:48" itemprop="dateCreated datePublished" datetime="2023-10-23T07:50:48+08:00">2023-10-23</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">更新于</span>
      <time title="修改时间：2023-11-18 08:32:16" itemprop="dateModified" datetime="2023-11-18T08:32:16+08:00">2023-11-18</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/Android/" itemprop="url" rel="index"><span itemprop="name">Android</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><p>本文创建了一个独立进程的service，通过bind方式来绑定服务，并通过binder与service进行交互。基于此来分析binder交互机制。</p>
<h2 id="Service的创建与使用"><a href="#Service的创建与使用" class="headerlink" title="Service的创建与使用"></a>Service的创建与使用</h2><p><strong>定义aidl接口：</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//IRemote.aidl</span></span><br><span class="line"><span class="keyword">interface</span> <span class="title class_">IRemote</span> &#123;</span><br><span class="line">    <span class="keyword">void</span> <span class="title function_">setCallback</span><span class="params">(RemoteCallback callback)</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">//RemoteCallback.aidl</span></span><br><span class="line"><span class="keyword">interface</span> <span class="title class_">RemoteCallback</span> &#123;</span><br><span class="line">    <span class="keyword">void</span> <span class="title function_">callback</span><span class="params">(String content)</span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>IRemote是service的对外接口，TalkCallback则用于从service回调client端，加入TalkCallback是为了更好的理解service和client相互调用的流程。<br>定义完aidl后，编译一遍项目，这样就可以生成对应的java类，对应的java类在<code>app/build/generated/aidl_source_output_dir</code>目录下。</p>
<p><strong>定义Service：</strong></p>
<figure class="highlight kotlin"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">RemoveService</span> <span class="title">extends</span> <span class="title">Service</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> RemoveService() &#123;&#125;</span><br><span class="line"></span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> IBinder onBind(Intent intent) &#123;</span><br><span class="line">        <span class="keyword">return</span> binder;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">final</span> IBinder binder = new IRemote.Stub() &#123;</span><br><span class="line">        <span class="meta">@Override</span></span><br><span class="line">        <span class="keyword">public</span> void setCallback(RemoteCallback callback) throws RemoteException &#123;</span><br><span class="line">            <span class="comment">//收到client端的函数调用，并回调client端</span></span><br><span class="line">            callback.callback(<span class="string">&quot;I&#x27;m Service&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>因为我们要定义一个独立进程的service，所以在AndroidManifest.xml要声明<code>android:process</code>属性，如下：</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">service</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:name</span>=<span class="string">&quot;.RemoveService&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:enabled</span>=<span class="string">&quot;true&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:exported</span>=<span class="string">&quot;false&quot;</span></span></span><br><span class="line"><span class="tag">    <span class="attr">android:process</span>=<span class="string">&quot;:remote&quot;</span>/&gt;</span></span><br></pre></td></tr></table></figure>

<p>这样一个独立进程的service就定义完成了，然后就可以使用了。</p>
<p><strong>绑定服务：</strong></p>
<p>我们在<code>MainActivity</code>中绑定service进行交互：</p>
<figure class="highlight kotlin"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">private</span> <span class="function"><span class="keyword">fun</span> <span class="title">testService</span><span class="params">()</span></span> &#123;</span><br><span class="line">    <span class="comment">//绑定服务</span></span><br><span class="line">    bindService(Intent(<span class="keyword">this</span>, RemoveService::<span class="keyword">class</span>.java), <span class="keyword">object</span> :</span><br><span class="line">        ServiceConnection &#123;</span><br><span class="line">        <span class="keyword">override</span> <span class="function"><span class="keyword">fun</span> <span class="title">onServiceConnected</span><span class="params">(</span></span></span><br><span class="line"><span class="params"><span class="function">            name: <span class="type">ComponentName</span>?,</span></span></span><br><span class="line"><span class="params"><span class="function">            service: <span class="type">IBinder</span>?</span></span></span><br><span class="line"><span class="params"><span class="function">        )</span></span> &#123;</span><br><span class="line">            <span class="keyword">val</span> proxy = IRemote.Stub.asInterface(service)</span><br><span class="line">            <span class="comment">//通过binder调用service的setCallback接口</span></span><br><span class="line">            proxy.setCallback(<span class="keyword">object</span> : RemoteCallback.Stub() &#123;</span><br><span class="line">                <span class="keyword">override</span> <span class="function"><span class="keyword">fun</span> <span class="title">callback</span><span class="params">(content: <span class="type">String</span>?)</span></span> &#123;</span><br><span class="line">                    <span class="comment">//收到service的回调消息</span></span><br><span class="line">                    Log.d(<span class="string">&quot;RemoteCallback&quot;</span>, <span class="string">&quot;callback message: <span class="variable">$content</span>&quot;</span>)</span><br><span class="line">                &#125;</span><br><span class="line">            &#125;)</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">override</span> <span class="function"><span class="keyword">fun</span> <span class="title">onServiceDisconnected</span><span class="params">(name: <span class="type">ComponentName</span>?)</span></span> &#123;&#125;</span><br><span class="line"></span><br><span class="line">    &#125;, Context.BIND_AUTO_CREATE)</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="IRemote-aidl的java实现"><a href="#IRemote-aidl的java实现" class="headerlink" title="IRemote.aidl的java实现"></a>IRemote.aidl的java实现</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">interface</span> <span class="title class_">IRemote</span> <span class="keyword">extends</span> <span class="title class_">android</span>.os.IInterface</span><br><span class="line">&#123;</span><br><span class="line">  <span class="keyword">public</span> <span class="keyword">void</span> <span class="title function_">talkAsync</span><span class="params">(me.rjy.android.demo.TalkCallback callback)</span> <span class="keyword">throws</span> android.os.RemoteException;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>IRemote.aidl生成的IRemote.java是一个接口类，具体的实现都在<code>IRemote.Stub</code>这个的静态内部类中，而且Stub还包含Proxy静态内部类。下面是它们的类图关系：</p>
<?xml version="1.0" encoding="us-ascii" standalone="no"?><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" contentStyleType="text/css" height="294px" preserveAspectRatio="none" style="width:497px;height:294px;background:#FFFFFF;" version="1.1" viewBox="0 0 497 294" width="497px" zoomAndPan="magnify"><defs/><g><!--class IInterface--><g id="elem_IInterface"><rect codeLine="1" fill="#F1F1F1" height="48" id="IInterface" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="179" x="7" y="7"/><ellipse cx="22" cy="23" fill="#B4A7E5" rx="11" ry="11" style="stroke:#181818;stroke-width:1.0;"/><path d="M22.9531,19.7813 L24.6719,19.7813 C25.0625,19.7813 25.25,19.75 25.375,19.6719 C25.6406,19.5156 25.7813,19.2344 25.7813,18.9375 C25.7813,18.6719 25.6719,18.4063 25.4375,18.2344 C25.2656,18.125 25.125,18.0938 24.6719,18.0938 L19.5313,18.0938 C19.0938,18.0938 18.9688,18.1094 18.8125,18.2031 C18.5625,18.3594 18.4063,18.6563 18.4063,18.9375 C18.4063,19.2188 18.5469,19.4688 18.7656,19.6406 C18.9219,19.75 19.1094,19.7813 19.5313,19.7813 L21.25,19.7813 L21.25,26.2969 L19.5313,26.2969 C19.0938,26.2969 18.9688,26.3125 18.8125,26.4219 C18.5625,26.5781 18.4063,26.8594 18.4063,27.1563 C18.4063,27.4063 18.5469,27.6719 18.7656,27.8281 C18.9219,27.9531 19.125,28 19.5313,28 L24.6719,28 C25.4219,28 25.7813,27.7188 25.7813,27.1563 C25.7813,26.875 25.6719,26.625 25.4375,26.4531 C25.2656,26.3281 25.125,26.2969 24.6719,26.2969 L22.9531,26.2969 L22.9531,19.7813 Z " fill="#000000"/><text fill="#000000" font-family="sans-serif" font-size="14" font-style="italic" lengthAdjust="spacing" textLength="147" x="36" y="27.8467">android.os.IInterface</text><line style="stroke:#181818;stroke-width:0.5;" x1="8" x2="185" y1="39" y2="39"/><line style="stroke:#181818;stroke-width:0.5;" x1="8" x2="185" y1="47" y2="47"/></g><!--class Binder--><g id="elem_Binder"><rect codeLine="2" fill="#F1F1F1" height="48" id="Binder" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="159" x="178" y="115"/><ellipse cx="193" cy="131" fill="#ADD1B2" rx="11" ry="11" style="stroke:#181818;stroke-width:1.0;"/><path d="M195.3438,126.6719 C194.4063,126.2344 193.8125,126.0938 192.9375,126.0938 C190.3125,126.0938 188.3125,128.1719 188.3125,130.8906 L188.3125,132.0156 C188.3125,134.5938 190.4219,136.4844 193.3125,136.4844 C194.5313,136.4844 195.6875,136.1875 196.4375,135.6406 C197.0156,135.2344 197.3438,134.7813 197.3438,134.3906 C197.3438,133.9375 196.9531,133.5469 196.4844,133.5469 C196.2656,133.5469 196.0625,133.625 195.875,133.8125 C195.4219,134.2969 195.4219,134.2969 195.2344,134.3906 C194.8125,134.6563 194.125,134.7813 193.3594,134.7813 C191.3125,134.7813 190.0156,133.6875 190.0156,131.9844 L190.0156,130.8906 C190.0156,129.1094 191.2656,127.7969 193,127.7969 C193.5781,127.7969 194.1875,127.9531 194.6563,128.2031 C195.1406,128.4844 195.3125,128.7031 195.4063,129.1094 C195.4688,129.5156 195.5,129.6406 195.6406,129.7656 C195.7813,129.9063 196.0156,130.0156 196.2344,130.0156 C196.5,130.0156 196.7656,129.875 196.9375,129.6563 C197.0469,129.5 197.0781,129.3125 197.0781,128.8906 L197.0781,127.4688 C197.0781,127.0313 197.0625,126.9063 196.9688,126.75 C196.8125,126.4844 196.5313,126.3438 196.2344,126.3438 C195.9375,126.3438 195.7344,126.4375 195.5156,126.75 L195.3438,126.6719 Z " fill="#000000"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="127" x="207" y="135.8467">android.os.Binder</text><line style="stroke:#181818;stroke-width:0.5;" x1="179" x2="336" y1="147" y2="147"/><line style="stroke:#181818;stroke-width:0.5;" x1="179" x2="336" y1="155" y2="155"/></g><!--class IBinder--><g id="elem_IBinder"><rect codeLine="3" fill="#F1F1F1" height="48" id="IBinder" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="163" x="265" y="7"/><ellipse cx="280" cy="23" fill="#ADD1B2" rx="11" ry="11" style="stroke:#181818;stroke-width:1.0;"/><path d="M282.3438,18.6719 C281.4063,18.2344 280.8125,18.0938 279.9375,18.0938 C277.3125,18.0938 275.3125,20.1719 275.3125,22.8906 L275.3125,24.0156 C275.3125,26.5938 277.4219,28.4844 280.3125,28.4844 C281.5313,28.4844 282.6875,28.1875 283.4375,27.6406 C284.0156,27.2344 284.3438,26.7813 284.3438,26.3906 C284.3438,25.9375 283.9531,25.5469 283.4844,25.5469 C283.2656,25.5469 283.0625,25.625 282.875,25.8125 C282.4219,26.2969 282.4219,26.2969 282.2344,26.3906 C281.8125,26.6563 281.125,26.7813 280.3594,26.7813 C278.3125,26.7813 277.0156,25.6875 277.0156,23.9844 L277.0156,22.8906 C277.0156,21.1094 278.2656,19.7969 280,19.7969 C280.5781,19.7969 281.1875,19.9531 281.6563,20.2031 C282.1406,20.4844 282.3125,20.7031 282.4063,21.1094 C282.4688,21.5156 282.5,21.6406 282.6406,21.7656 C282.7813,21.9063 283.0156,22.0156 283.2344,22.0156 C283.5,22.0156 283.7656,21.875 283.9375,21.6563 C284.0469,21.5 284.0781,21.3125 284.0781,20.8906 L284.0781,19.4688 C284.0781,19.0313 284.0625,18.9063 283.9688,18.75 C283.8125,18.4844 283.5313,18.3438 283.2344,18.3438 C282.9375,18.3438 282.7344,18.4375 282.5156,18.75 L282.3438,18.6719 Z " fill="#000000"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="131" x="294" y="27.8467">android.os.IBinder</text><line style="stroke:#181818;stroke-width:0.5;" x1="266" x2="427" y1="39" y2="39"/><line style="stroke:#181818;stroke-width:0.5;" x1="266" x2="427" y1="47" y2="47"/></g><!--class IRemote--><g id="elem_IRemote"><rect codeLine="5" fill="#F1F1F1" height="48" id="IRemote" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="92" x="50.5" y="115"/><ellipse cx="65.5" cy="131" fill="#B4A7E5" rx="11" ry="11" style="stroke:#181818;stroke-width:1.0;"/><path d="M66.4531,127.7813 L68.1719,127.7813 C68.5625,127.7813 68.75,127.75 68.875,127.6719 C69.1406,127.5156 69.2813,127.2344 69.2813,126.9375 C69.2813,126.6719 69.1719,126.4063 68.9375,126.2344 C68.7656,126.125 68.625,126.0938 68.1719,126.0938 L63.0313,126.0938 C62.5938,126.0938 62.4688,126.1094 62.3125,126.2031 C62.0625,126.3594 61.9063,126.6563 61.9063,126.9375 C61.9063,127.2188 62.0469,127.4688 62.2656,127.6406 C62.4219,127.75 62.6094,127.7813 63.0313,127.7813 L64.75,127.7813 L64.75,134.2969 L63.0313,134.2969 C62.5938,134.2969 62.4688,134.3125 62.3125,134.4219 C62.0625,134.5781 61.9063,134.8594 61.9063,135.1563 C61.9063,135.4063 62.0469,135.6719 62.2656,135.8281 C62.4219,135.9531 62.625,136 63.0313,136 L68.1719,136 C68.9219,136 69.2813,135.7188 69.2813,135.1563 C69.2813,134.875 69.1719,134.625 68.9375,134.4531 C68.7656,134.3281 68.625,134.2969 68.1719,134.2969 L66.4531,134.2969 L66.4531,127.7813 Z " fill="#000000"/><text fill="#000000" font-family="sans-serif" font-size="14" font-style="italic" lengthAdjust="spacing" textLength="60" x="79.5" y="135.8467">IRemote</text><line style="stroke:#181818;stroke-width:0.5;" x1="51.5" x2="141.5" y1="147" y2="147"/><line style="stroke:#181818;stroke-width:0.5;" x1="51.5" x2="141.5" y1="155" y2="155"/></g><!--class Stub--><g id="elem_Stub"><rect codeLine="7" fill="#F1F1F1" height="48" id="Stub" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="128" x="51.5" y="231"/><ellipse cx="66.5" cy="247" fill="#A9DCDF" rx="11" ry="11" style="stroke:#181818;stroke-width:1.0;"/><path d="M68.5781,248.8125 L68.9688,249.7969 L68.5781,249.7969 C68.125,249.7969 68.0156,249.8125 67.8594,249.9219 C67.6094,250.0781 67.4531,250.3594 67.4531,250.6563 C67.4531,250.9219 67.5938,251.1719 67.8125,251.3281 C67.9531,251.4531 68.1563,251.5 68.5781,251.5 L70.9375,251.5 C71.2969,251.5 71.5156,251.4688 71.6563,251.375 C71.9063,251.2344 72.0625,250.9375 72.0625,250.6563 C72.0625,250.375 71.9219,250.125 71.7031,249.9688 C71.5313,249.8281 71.375,249.7969 70.9063,249.7969 L67.5156,241.5938 L63.8438,241.5938 C63.3906,241.5938 63.2656,241.6094 63.1094,241.7031 C62.8594,241.875 62.7031,242.1563 62.7031,242.4375 C62.7031,242.7188 62.8438,242.9688 63.0625,243.1406 C63.2344,243.25 63.4063,243.2813 63.8438,243.2813 L64.9219,243.2813 L62.2813,249.7969 C61.8594,249.7969 61.7031,249.8125 61.5469,249.9219 C61.2969,250.0781 61.1406,250.3594 61.1406,250.6563 C61.1406,251.2188 61.5156,251.5 62.2656,251.5 L64.5313,251.5 C64.8906,251.5 65.1094,251.4688 65.2344,251.375 C65.5,251.2344 65.6406,250.9375 65.6406,250.6563 C65.6406,250.375 65.5156,250.125 65.2969,249.9531 C65.125,249.8281 64.9844,249.7969 64.5313,249.7969 L64.1406,249.7969 L64.5313,248.8125 L68.5781,248.8125 Z M67.875,247.1094 L65.2031,247.1094 L66.5469,243.8438 L67.875,247.1094 Z " fill="#000000"/><text fill="#000000" font-family="sans-serif" font-size="14" font-style="italic" lengthAdjust="spacing" textLength="96" x="80.5" y="251.8467">IRemote.Stub</text><line style="stroke:#181818;stroke-width:0.5;" x1="52.5" x2="178.5" y1="263" y2="263"/><line style="stroke:#181818;stroke-width:0.5;" x1="52.5" x2="178.5" y1="271" y2="271"/></g><!--class Proxy--><g id="elem_Proxy"><rect codeLine="11" fill="#F1F1F1" height="64.2969" id="Proxy" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="238" x="252.5" y="223"/><ellipse cx="294.5" cy="239" fill="#ADD1B2" rx="11" ry="11" style="stroke:#181818;stroke-width:1.0;"/><path d="M296.8438,234.6719 C295.9063,234.2344 295.3125,234.0938 294.4375,234.0938 C291.8125,234.0938 289.8125,236.1719 289.8125,238.8906 L289.8125,240.0156 C289.8125,242.5938 291.9219,244.4844 294.8125,244.4844 C296.0313,244.4844 297.1875,244.1875 297.9375,243.6406 C298.5156,243.2344 298.8438,242.7813 298.8438,242.3906 C298.8438,241.9375 298.4531,241.5469 297.9844,241.5469 C297.7656,241.5469 297.5625,241.625 297.375,241.8125 C296.9219,242.2969 296.9219,242.2969 296.7344,242.3906 C296.3125,242.6563 295.625,242.7813 294.8594,242.7813 C292.8125,242.7813 291.5156,241.6875 291.5156,239.9844 L291.5156,238.8906 C291.5156,237.1094 292.7656,235.7969 294.5,235.7969 C295.0781,235.7969 295.6875,235.9531 296.1563,236.2031 C296.6406,236.4844 296.8125,236.7031 296.9063,237.1094 C296.9688,237.5156 297,237.6406 297.1406,237.7656 C297.2813,237.9063 297.5156,238.0156 297.7344,238.0156 C298,238.0156 298.2656,237.875 298.4375,237.6563 C298.5469,237.5 298.5781,237.3125 298.5781,236.8906 L298.5781,235.4688 C298.5781,235.0313 298.5625,234.9063 298.4688,234.75 C298.3125,234.4844 298.0313,234.3438 297.7344,234.3438 C297.4375,234.3438 297.2344,234.4375 297.0156,234.75 L296.8438,234.6719 Z " fill="#000000"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="146" x="314.5" y="243.8467">IRemote.Stub.Proxy</text><line style="stroke:#181818;stroke-width:0.5;" x1="253.5" x2="489.5" y1="255" y2="255"/><rect fill="none" height="6" style="stroke:#C82930;stroke-width:1.0;" width="6" x="260.5" y="265.6484"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="212" x="272.5" y="271.9951">android.os.IBinder mRemote;</text><line style="stroke:#181818;stroke-width:0.5;" x1="253.5" x2="489.5" y1="279.2969" y2="279.2969"/></g><!--reverse link IBinder to Binder--><g id="link_IBinder_Binder"><path codeLine="4" d="M315.6136,68.7853 C300.7876,86.4433 291.974,96.941 277.081,114.678 " fill="none" id="IBinder-backto-Binder" style="stroke:#181818;stroke-width:1.0;"/><polygon fill="none" points="327.188,55,311.0185,64.9271,320.2087,72.6434,327.188,55" style="stroke:#181818;stroke-width:1.0;"/></g><!--reverse link IInterface to IRemote--><g id="link_IInterface_IRemote"><path codeLine="6" d="M96.5,73 C96.5,90.658 96.5,96.941 96.5,114.678 " fill="none" id="IInterface-backto-IRemote" style="stroke:#181818;stroke-width:1.0;"/><polygon fill="none" points="96.5,55,90.5,73,102.5,73,96.5,55" style="stroke:#181818;stroke-width:1.0;"/></g><!--reverse link Binder to Stub--><g id="link_Binder_Stub"><path codeLine="8" d="M214.7276,174.3385 C190.0916,194.1165 168.817,211.1961 144.193,230.9647 " fill="none" id="Binder-backto-Stub" style="stroke:#181818;stroke-width:1.0;"/><polygon fill="none" points="228.764,163.07,210.9714,169.6597,218.4838,179.0173,228.764,163.07" style="stroke:#181818;stroke-width:1.0;"/></g><!--reverse link IRemote to Stub--><g id="link_IRemote_Stub"><path codeLine="9" d="M103.3041,180.8251 C106.6005,200.6031 108.366,211.1961 111.661,230.9647 " fill="none" id="IRemote-backto-Stub" style="stroke:#181818;stroke-width:1.0;stroke-dasharray:7.0,7.0;"/><polygon fill="none" points="100.3449,163.07,97.3858,181.8115,109.2225,179.8387,100.3449,163.07" style="stroke:#181818;stroke-width:1.0;"/></g><!--reverse link IRemote to Proxy--><g id="link_IRemote_Proxy"><path codeLine="14" d="M159.1569,165.9741 C201.1279,183.3731 247.282,202.5062 296.496,222.9076 " fill="none" id="IRemote-backto-Proxy" style="stroke:#181818;stroke-width:1.0;stroke-dasharray:7.0,7.0;"/><polygon fill="none" points="142.529,159.081,156.8592,171.5167,161.4545,160.4314,142.529,159.081" style="stroke:#181818;stroke-width:1.0;"/></g><!--link IBinder to Proxy--><g id="link_IBinder_Proxy"><path codeLine="15" d="M349.128,55.338 C353.654,95.526 361.576,165.8804 366.648,210.9159 " fill="none" id="IBinder-to-Proxy" style="stroke:#181818;stroke-width:1.0;"/><polygon fill="#181818" points="367.991,222.8405,371.2944,216.4305,366.648,210.9159,363.3446,217.3259,367.991,222.8405" style="stroke:#181818;stroke-width:1.0;"/></g><!--SRC=[ROzD3W8X38NtFKN3qeHUG5Vks1az0GEOa3YG02OQnxixyZF3HDdm-kNrjRKkcN2LoY2JJWTlDVb8GZJCK4PSIr0t6UF6UxHEcv1zHVxnYCvKDTGV3nFdhTCksuYp6Nqoi8ukniRb45EGAY6hX2xfFkG1Nm5zU0RLDXEYGjgUVG2TWdyySqnM-0AS7_-z1SVIjeVtDZltBTTnlYla0m00]--></g></svg>

<p>下面是<code>IRemote</code>的内部类<code>Stub</code>的源码实现：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">abstract</span> <span class="keyword">class</span> <span class="title class_">Stub</span> <span class="keyword">extends</span> <span class="title class_">android</span>.os.Binder <span class="keyword">implements</span> <span class="title class_">me</span>.rjy.android.demo.IRemote</span><br><span class="line">&#123;</span><br><span class="line">  <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> java.lang.<span class="type">String</span> <span class="variable">DESCRIPTOR</span> <span class="operator">=</span> <span class="string">&quot;me.rjy.android.demo.IRemote&quot;</span>;</span><br><span class="line">  <span class="keyword">public</span> <span class="title function_">Stub</span><span class="params">()</span></span><br><span class="line">  &#123;</span><br><span class="line">    <span class="built_in">this</span>.attachInterface(<span class="built_in">this</span>, DESCRIPTOR);</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">public</span> <span class="keyword">static</span> me.rjy.android.demo.IRemote <span class="title function_">asInterface</span><span class="params">(android.os.IBinder obj)</span></span><br><span class="line">  &#123;</span><br><span class="line">    <span class="keyword">if</span> ((obj==<span class="literal">null</span>)) &#123;</span><br><span class="line">      <span class="keyword">return</span> <span class="literal">null</span>;</span><br><span class="line">    &#125;</span><br><span class="line">    android.os.<span class="type">IInterface</span> <span class="variable">iin</span> <span class="operator">=</span> obj.queryLocalInterface(DESCRIPTOR);</span><br><span class="line">    <span class="keyword">if</span> (((iin!=<span class="literal">null</span>)&amp;&amp;(iin <span class="keyword">instanceof</span> me.rjy.android.demo.IRemote))) &#123;</span><br><span class="line">      <span class="keyword">return</span> ((me.rjy.android.demo.IRemote)iin);</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">return</span> <span class="keyword">new</span> <span class="title class_">me</span>.rjy.android.demo.IRemote.Stub.Proxy(obj);</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="meta">@Override</span> <span class="keyword">public</span> android.os.IBinder <span class="title function_">asBinder</span><span class="params">()</span> &#123; <span class="keyword">return</span> <span class="built_in">this</span>; &#125;</span><br><span class="line"></span><br><span class="line">  <span class="comment">//onTransact是binder跨进程通信的关键函数</span></span><br><span class="line">  <span class="meta">@Override</span> <span class="keyword">public</span> <span class="type">boolean</span> <span class="title function_">onTransact</span><span class="params">(<span class="type">int</span> code, android.os.Parcel data, android.os.Parcel reply, <span class="type">int</span> flags)</span> <span class="keyword">throws</span> android.os.RemoteException</span><br><span class="line">  &#123;</span><br><span class="line">    java.lang.<span class="type">String</span> <span class="variable">descriptor</span> <span class="operator">=</span> DESCRIPTOR;</span><br><span class="line">    <span class="keyword">switch</span> (code)</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="keyword">case</span> INTERFACE_TRANSACTION:</span><br><span class="line">      &#123;</span><br><span class="line">        reply.writeString(descriptor);</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="keyword">case</span> TRANSACTION_setCallback:</span><br><span class="line">      &#123;</span><br><span class="line">        data.enforceInterface(descriptor);</span><br><span class="line">        me.rjy.android.demo.RemoteCallback _arg0;</span><br><span class="line">        _arg0 = me.rjy.android.demo.RemoteCallback.Stub.asInterface(data.readStrongBinder());</span><br><span class="line">        <span class="built_in">this</span>.setCallback(_arg0); <span class="comment">//调用RemoveService.binder.setCallback</span></span><br><span class="line">        reply.writeNoException();</span><br><span class="line">        <span class="keyword">return</span> <span class="literal">true</span>;</span><br><span class="line">      &#125;</span><br><span class="line">      <span class="keyword">default</span>:</span><br><span class="line">      &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="built_in">super</span>.onTransact(code, data, reply, flags);</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">static</span> <span class="keyword">final</span> <span class="type">int</span> <span class="variable">TRANSACTION_setCallback</span> <span class="operator">=</span> (android.os.IBinder.FIRST_CALL_TRANSACTION + <span class="number">0</span>);</span><br><span class="line">  ......</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>RemoveService的<code>IBinder onBind(Intent intent)</code>方法返回的就是<code>IRemote.Stub</code>的实现类：</p>
<figure class="highlight kotlin"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">RemoveService</span> <span class="title">extends</span> <span class="title">Service</span> &#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="keyword">public</span> IBinder onBind(Intent intent) &#123;</span><br><span class="line">        <span class="keyword">return</span> binder;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">final</span> IBinder binder = new IRemote.Stub() &#123;</span><br><span class="line">        <span class="meta">@Override</span></span><br><span class="line">        <span class="keyword">public</span> void setCallback(RemoteCallback callback) throws RemoteException &#123;</span><br><span class="line">            callback.callback(<span class="string">&quot;I&#x27;m Service&quot;</span>);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>所以，Stub是在服务端。</p>
<h2 id="Service绑定流程"><a href="#Service绑定流程" class="headerlink" title="Service绑定流程"></a>Service绑定流程</h2><pre class="mermaid">

zenuml
group App {
    &lt;&lt;App&gt;&gt; MainActivity
    &lt;&lt;App&gt;&gt; ContextImpl
    &lt;&lt;App&gt;&gt; ServiceDispatcher
    &lt;&lt;App&gt;&gt; InnerConnection
}
group AMS {
    &lt;&lt;AMS&gt;&gt; ActivityManagerService
    &lt;&lt;AMS&gt;&gt; ActiveServices
}
MainActivity-&gt;ContextImpl.bindService {
    bindServiceCommon {
        &quot;LoadedApk.getServiceDispatcher&quot; {
            new ServiceDispatcher() {
                new InnerConnection()
            }
        }
        ActivityManagerService.bindServiceInstance {
            ActiveServices.bindServiceLocked {
                retrieveServiceLocked
                InnerConnection.connected {
                    ServiceDispatcher.connected {
                        &quot;Handler.post RunConnection()&quot;
                    }
                }
            }
        }
    }
}
ServiceDispatcher-&gt;ServiceDispatcher.&quot;RunConnection.run() -&gt; doConnected&quot; {
    MainActivity.onServiceConnected
}
</pre>

<p>service的绑定过程用到了ServiceDispatcher和InnerConnection，来看下两者的类图结构：</p>
<?xml version="1.0" encoding="us-ascii" standalone="no"?><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" contentStyleType="text/css" height="170px" preserveAspectRatio="none" style="width:611px;height:170px;background:#FFFFFF;" version="1.1" viewBox="0 0 611 170" width="611px" zoomAndPan="magnify"><defs/><g><!--class InnerConnection--><g id="elem_InnerConnection"><rect codeLine="1" fill="#F1F1F1" height="48" id="InnerConnection" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="375" x="7" y="115"/><ellipse cx="22" cy="131" fill="#ADD1B2" rx="11" ry="11" style="stroke:#181818;stroke-width:1.0;"/><path d="M24.3438,126.6719 C23.4063,126.2344 22.8125,126.0938 21.9375,126.0938 C19.3125,126.0938 17.3125,128.1719 17.3125,130.8906 L17.3125,132.0156 C17.3125,134.5938 19.4219,136.4844 22.3125,136.4844 C23.5313,136.4844 24.6875,136.1875 25.4375,135.6406 C26.0156,135.2344 26.3438,134.7813 26.3438,134.3906 C26.3438,133.9375 25.9531,133.5469 25.4844,133.5469 C25.2656,133.5469 25.0625,133.625 24.875,133.8125 C24.4219,134.2969 24.4219,134.2969 24.2344,134.3906 C23.8125,134.6563 23.125,134.7813 22.3594,134.7813 C20.3125,134.7813 19.0156,133.6875 19.0156,131.9844 L19.0156,130.8906 C19.0156,129.1094 20.2656,127.7969 22,127.7969 C22.5781,127.7969 23.1875,127.9531 23.6563,128.2031 C24.1406,128.4844 24.3125,128.7031 24.4063,129.1094 C24.4688,129.5156 24.5,129.6406 24.6406,129.7656 C24.7813,129.9063 25.0156,130.0156 25.2344,130.0156 C25.5,130.0156 25.7656,129.875 25.9375,129.6563 C26.0469,129.5 26.0781,129.3125 26.0781,128.8906 L26.0781,127.4688 C26.0781,127.0313 26.0625,126.9063 25.9688,126.75 C25.8125,126.4844 25.5313,126.3438 25.2344,126.3438 C24.9375,126.3438 24.7344,126.4375 24.5156,126.75 L24.3438,126.6719 Z " fill="#000000"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="343" x="36" y="135.8467">LoadedApk.ServiceDispatcher.InnerConnection</text><line style="stroke:#181818;stroke-width:0.5;" x1="8" x2="381" y1="147" y2="147"/><line style="stroke:#181818;stroke-width:0.5;" x1="8" x2="381" y1="155" y2="155"/></g><!--class Stub--><g id="elem_Stub"><rect codeLine="2" fill="#F1F1F1" height="48" id="Stub" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="206" x="91.5" y="7"/><ellipse cx="106.5" cy="23" fill="#A9DCDF" rx="11" ry="11" style="stroke:#181818;stroke-width:1.0;"/><path d="M108.5781,24.8125 L108.9688,25.7969 L108.5781,25.7969 C108.125,25.7969 108.0156,25.8125 107.8594,25.9219 C107.6094,26.0781 107.4531,26.3594 107.4531,26.6563 C107.4531,26.9219 107.5938,27.1719 107.8125,27.3281 C107.9531,27.4531 108.1563,27.5 108.5781,27.5 L110.9375,27.5 C111.2969,27.5 111.5156,27.4688 111.6563,27.375 C111.9063,27.2344 112.0625,26.9375 112.0625,26.6563 C112.0625,26.375 111.9219,26.125 111.7031,25.9688 C111.5313,25.8281 111.375,25.7969 110.9063,25.7969 L107.5156,17.5938 L103.8438,17.5938 C103.3906,17.5938 103.2656,17.6094 103.1094,17.7031 C102.8594,17.875 102.7031,18.1563 102.7031,18.4375 C102.7031,18.7188 102.8438,18.9688 103.0625,19.1406 C103.2344,19.25 103.4063,19.2813 103.8438,19.2813 L104.9219,19.2813 L102.2813,25.7969 C101.8594,25.7969 101.7031,25.8125 101.5469,25.9219 C101.2969,26.0781 101.1406,26.3594 101.1406,26.6563 C101.1406,27.2188 101.5156,27.5 102.2656,27.5 L104.5313,27.5 C104.8906,27.5 105.1094,27.4688 105.2344,27.375 C105.5,27.2344 105.6406,26.9375 105.6406,26.6563 C105.6406,26.375 105.5156,26.125 105.2969,25.9531 C105.125,25.8281 104.9844,25.7969 104.5313,25.7969 L104.1406,25.7969 L104.5313,24.8125 L108.5781,24.8125 Z M107.875,23.1094 L105.2031,23.1094 L106.5469,19.8438 L107.875,23.1094 Z " fill="#000000"/><text fill="#000000" font-family="sans-serif" font-size="14" font-style="italic" lengthAdjust="spacing" textLength="174" x="120.5" y="27.8467">IServiceConnection.Stub</text><line style="stroke:#181818;stroke-width:0.5;" x1="92.5" x2="296.5" y1="39" y2="39"/><line style="stroke:#181818;stroke-width:0.5;" x1="92.5" x2="296.5" y1="47" y2="47"/></g><!--class ServiceDispatcher--><g id="elem_ServiceDispatcher"><rect codeLine="5" fill="#F1F1F1" height="48" id="ServiceDispatcher" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="250" x="354.5" y="7"/><ellipse cx="369.5" cy="23" fill="#ADD1B2" rx="11" ry="11" style="stroke:#181818;stroke-width:1.0;"/><path d="M371.8438,18.6719 C370.9063,18.2344 370.3125,18.0938 369.4375,18.0938 C366.8125,18.0938 364.8125,20.1719 364.8125,22.8906 L364.8125,24.0156 C364.8125,26.5938 366.9219,28.4844 369.8125,28.4844 C371.0313,28.4844 372.1875,28.1875 372.9375,27.6406 C373.5156,27.2344 373.8438,26.7813 373.8438,26.3906 C373.8438,25.9375 373.4531,25.5469 372.9844,25.5469 C372.7656,25.5469 372.5625,25.625 372.375,25.8125 C371.9219,26.2969 371.9219,26.2969 371.7344,26.3906 C371.3125,26.6563 370.625,26.7813 369.8594,26.7813 C367.8125,26.7813 366.5156,25.6875 366.5156,23.9844 L366.5156,22.8906 C366.5156,21.1094 367.7656,19.7969 369.5,19.7969 C370.0781,19.7969 370.6875,19.9531 371.1563,20.2031 C371.6406,20.4844 371.8125,20.7031 371.9063,21.1094 C371.9688,21.5156 372,21.6406 372.1406,21.7656 C372.2813,21.9063 372.5156,22.0156 372.7344,22.0156 C373,22.0156 373.2656,21.875 373.4375,21.6563 C373.5469,21.5 373.5781,21.3125 373.5781,20.8906 L373.5781,19.4688 C373.5781,19.0313 373.5625,18.9063 373.4688,18.75 C373.3125,18.4844 373.0313,18.3438 372.7344,18.3438 C372.4375,18.3438 372.2344,18.4375 372.0156,18.75 L371.8438,18.6719 Z " fill="#000000"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="218" x="383.5" y="27.8467">LoadedApk.ServiceDispatcher</text><line style="stroke:#181818;stroke-width:0.5;" x1="355.5" x2="603.5" y1="39" y2="39"/><line style="stroke:#181818;stroke-width:0.5;" x1="355.5" x2="603.5" y1="47" y2="47"/></g><!--class ServiceConnection--><g id="elem_ServiceConnection"><rect fill="#F1F1F1" height="48" id="ServiceConnection" rx="2.5" ry="2.5" style="stroke:#181818;stroke-width:0.5;" width="168" x="417.5" y="115"/><ellipse cx="432.5" cy="131" fill="#ADD1B2" rx="11" ry="11" style="stroke:#181818;stroke-width:1.0;"/><path d="M434.8438,126.6719 C433.9063,126.2344 433.3125,126.0938 432.4375,126.0938 C429.8125,126.0938 427.8125,128.1719 427.8125,130.8906 L427.8125,132.0156 C427.8125,134.5938 429.9219,136.4844 432.8125,136.4844 C434.0313,136.4844 435.1875,136.1875 435.9375,135.6406 C436.5156,135.2344 436.8438,134.7813 436.8438,134.3906 C436.8438,133.9375 436.4531,133.5469 435.9844,133.5469 C435.7656,133.5469 435.5625,133.625 435.375,133.8125 C434.9219,134.2969 434.9219,134.2969 434.7344,134.3906 C434.3125,134.6563 433.625,134.7813 432.8594,134.7813 C430.8125,134.7813 429.5156,133.6875 429.5156,131.9844 L429.5156,130.8906 C429.5156,129.1094 430.7656,127.7969 432.5,127.7969 C433.0781,127.7969 433.6875,127.9531 434.1563,128.2031 C434.6406,128.4844 434.8125,128.7031 434.9063,129.1094 C434.9688,129.5156 435,129.6406 435.1406,129.7656 C435.2813,129.9063 435.5156,130.0156 435.7344,130.0156 C436,130.0156 436.2656,129.875 436.4375,129.6563 C436.5469,129.5 436.5781,129.3125 436.5781,128.8906 L436.5781,127.4688 C436.5781,127.0313 436.5625,126.9063 436.4688,126.75 C436.3125,126.4844 436.0313,126.3438 435.7344,126.3438 C435.4375,126.3438 435.2344,126.4375 435.0156,126.75 L434.8438,126.6719 Z " fill="#000000"/><text fill="#000000" font-family="sans-serif" font-size="14" lengthAdjust="spacing" textLength="136" x="446.5" y="135.8467">ServiceConnection</text><line style="stroke:#181818;stroke-width:0.5;" x1="418.5" x2="584.5" y1="147" y2="147"/><line style="stroke:#181818;stroke-width:0.5;" x1="418.5" x2="584.5" y1="155" y2="155"/></g><!--reverse link Stub to InnerConnection--><g id="link_Stub_InnerConnection"><path codeLine="3" d="M194.5,73 C194.5,90.6584 194.5,96.9408 194.5,114.6784 " fill="none" id="Stub-backto-InnerConnection" style="stroke:#181818;stroke-width:1.0;"/><polygon fill="none" points="194.5,55,188.5,73,200.5,73,194.5,55" style="stroke:#181818;stroke-width:1.0;"/></g><!--reverse link ServiceDispatcher to ServiceConnection--><g id="link_ServiceDispatcher_ServiceConnection"><path codeLine="8" d="M486.7126,66.7496 C490.3776,84.408 492.978,96.9408 496.66,114.6784 " fill="none" id="ServiceDispatcher-backto-ServiceConnection" style="stroke:#181818;stroke-width:1.0;"/><polygon fill="#181818" points="484.274,55,481.5768,61.6877,486.7126,66.7496,489.4098,60.0619,484.274,55" style="stroke:#181818;stroke-width:1.0;"/></g><!--reverse link ServiceDispatcher to InnerConnection--><g id="link_ServiceDispatcher_InnerConnection"><path codeLine="9" d="M406.4118,59.1833 C358.7718,76.9026 304.445,97.1083 256.712,114.8617 " fill="none" id="ServiceDispatcher-backto-InnerConnection" style="stroke:#181818;stroke-width:1.0;"/><polygon fill="#181818" points="417.659,55,410.641,53.3426,406.4118,59.1833,413.4298,60.8407,417.659,55" style="stroke:#181818;stroke-width:1.0;"/></g><!--SRC=[Iyv9B2vMKFB9Jqn9JN4ioDOBJYqgoqnETSaiBaWiISv8BTBppCjBBNBE1vB99PdvUKeAYSKAQ69SYKd59KM9oIKAIfvG0n2IUi4bfKbWNI065uXGiAdHrSKmX2kPe6CWng2BAbHpSTLoOGfh0It1S0uETMYE0G00]--></g></svg>

<p>IServiceConnection是一个binder接口，app是service端，ams是client端，当绑定成功后，ams通过IServiceConnection来通知app绑定成功。</p>
<h2 id="writeStrongBinder"><a href="#writeStrongBinder" class="headerlink" title="writeStrongBinder"></a>writeStrongBinder</h2><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//frameworks\base\core\java\android\os\Parcel.java</span></span><br><span class="line">    <span class="meta">@FastNative</span></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">native</span> <span class="keyword">void</span> <span class="title function_">nativeWriteStrongBinder</span><span class="params">(<span class="type">long</span> nativePtr, IBinder val)</span>;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">final</span> <span class="keyword">void</span> <span class="title function_">writeStrongBinder</span><span class="params">(IBinder val)</span> &#123;</span><br><span class="line">        nativeWriteStrongBinder(mNativePtr, val);</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>

<p>nativeWriteStrongBinder是native函数，对应的c++的实现源码如下：</p>
<figure class="highlight c++"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//android_os_Parcel.cpp</span></span><br><span class="line"><span class="function"><span class="type">static</span> <span class="type">void</span> <span class="title">android_os_Parcel_writeStrongBinder</span><span class="params">(JNIEnv* env, jclass clazz, jlong nativePtr, jobject object)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">    Parcel* parcel = <span class="built_in">reinterpret_cast</span>&lt;Parcel*&gt;(nativePtr);</span><br><span class="line">    <span class="keyword">if</span> (parcel != <span class="literal">NULL</span>) &#123;</span><br><span class="line">        <span class="type">const</span> <span class="type">status_t</span> err = parcel-&gt;<span class="built_in">writeStrongBinder</span>(<span class="built_in">ibinderForJavaObject</span>(env, object));</span><br><span class="line">        <span class="keyword">if</span> (err != NO_ERROR) &#123;</span><br><span class="line">            <span class="built_in">signalExceptionForError</span>(env, clazz, err);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/Android/" rel="tag"># Android</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/posts/90f60c72.html" rel="prev" title="Android WindowManager与Window">
                  <i class="fa fa-angle-left"></i> Android WindowManager与Window
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/posts/abd111cb.html" rel="next" title="Java动态代理">
                  Java动态代理 <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="copyright">
    &copy; 
    <span itemprop="copyrightYear">2024</span>
    <span class="with-love">
      <i class="fa fa-heart"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">Jason</span>
  </div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener external nofollow noreferrer" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/pisces/" rel="noopener external nofollow noreferrer" target="_blank">NexT.Pisces</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/next-theme-pjax/0.6.0/pjax.min.js" integrity="sha256-vxLn1tSKWD4dqbMRyv940UYw4sXgMtYcK6reefzZrao=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.24/fancybox/fancybox.umd.js" integrity="sha256-oyhjPiYRWGXaAt+ny/mTMWOnN1GBoZDUQnzzgC7FRI4=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script><script src="/js/pjax.js"></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.1/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>


  <script class="next-config" data-name="mermaid" type="application/json">{"enable":true,"theme":{"light":"default","dark":"dark"},"js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/mermaid/10.5.0/mermaid.min.js","integrity":"sha256-K7oJiQlDulzl24ZUFOywuYme1JqBBvQzK6m8qHjt9Gk="}}</script>
  <script type="module" src="/js/zenuml-definition-074a43fa.js"></script>
  <script type="module" src="/js/mermaid-zenuml.esm.min.mjs"></script>
  <script src="/js/third-party/tags/mermaid.js"></script>


  <script src="/js/third-party/fancybox.js"></script>



  




  

  <script class="next-config" data-name="enableMath" type="application/json">true</script><script class="next-config" data-name="mathjax" type="application/json">{"enable":true,"tags":"none","js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.js","integrity":"sha256-MASABpB4tYktI2Oitl4t+78w/lyA+D7b/s9GEP0JOGI="}}</script>
<script src="/js/third-party/math/mathjax.js"></script>



</body>
</html>
